package com.lmpop.trade.ti_brand;

import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import com.lmpop.frame.QueryList;
import com.lmpop.frame.SysMap;
import com.lmpop.frame.TradeInterf;
import com.lmpop.frame.dao.DbmInterf;
import com.lmpop.frame.exp.ApplicationException;

public class Ti_brandInfo implements TradeInterf
{
  QueryList queryList;

  public Ti_brandInfo()
  {
    this.queryList = new QueryList();
  }

  public SysMap insert(DbmInterf dbmMgr, SysMap map)
  {
    Ti_brand ti_brand = new Ti_brand();
    ti_brand.setInfo_id(map.getString("info_id"));
    if (map.getString("is_platform").equals("0"))
    {
      ti_brand.setCust_id(map.getString("session_cust_id"));
    }
    else
    {
      ti_brand.setCust_id("111111111111111");
    }
    if ((map.getString("cust_id") != null) && (!map.getString("cust_id").equals("")))
    {
      ti_brand.setCust_id(map.getString("cust_id"));
    }
    ti_brand.setClass_id_group(map.getString("class_id_group"));
    ti_brand.setTitle(map.getString("title"));
    ti_brand.setContent(map.getString("content"));
    ti_brand.setSite_url(map.getString("site_url"));
    ti_brand.setSort_no(map.getInt("sort_no"));
    ti_brand.setIs_show(map.getString("is_show"));
    ti_brand.setUser_id(map.getString("session_user_id"));
    ti_brand.setPyKey(map.getString("pyKey"));
    String result_code = "0";
    try {
      insert(dbmMgr, ti_brand);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void insert(DbmInterf dbmMgr, Ti_brand ti_brand)
    throws ApplicationException
  {
    dbmMgr.update("insertTi_brand", ti_brand);
  }

  public SysMap delete(DbmInterf dbmMgr, SysMap map)
  {
    String pkid_string = map.getString("pkid");
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++) {
          if (pkStr[i].equals("")) continue; delete(dbmMgr, pkStr[i]);
        }
      }
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    map.setString("attach_root_id", pkid_string);
    map.setString("attach_sign", "delete");

    return map;
  }

  public void delete(DbmInterf dbmMgr, String pkid)
    throws ApplicationException
  {
    dbmMgr.update("deleteTi_brand", pkid);
  }

  public SysMap update(DbmInterf dbmMgr, SysMap map)
  {
    Ti_brand ti_brand = new Ti_brand();
    ti_brand.setInfo_id(map.getString("info_id"));

    ti_brand.setClass_id_group(map.getString("class_id_group"));
    ti_brand.setTitle(map.getString("title"));
    ti_brand.setContent(map.getString("content"));
    ti_brand.setSite_url(map.getString("site_url"));
    ti_brand.setSort_no(map.getInt("sort_no"));
    ti_brand.setIs_show(map.getString("is_show"));

    ti_brand.setPyKey(map.getString("pyKey"));
    String result_code = "0";
    try {
      update(dbmMgr, ti_brand);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void update(DbmInterf dbmMgr, Ti_brand ti_brand)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_brand", ti_brand);
  }

  public List getListByPage(Object obj, int start, int limit)
  {
    Map params = (Hashtable)obj;
    if (start == 0)
      start = 0;
    else {
      start = (start - 1) * limit;
    }
    params.put("start", Integer.valueOf(start));
    params.put("limit", Integer.valueOf(limit));
    return this.queryList.getList("getTi_brandList", params);
  }

  public int getCountByObj(Object obj)
  {
    Map params = (Hashtable)obj;
    List list = this.queryList.getList("getTi_brandSum", params);
    int count = 0;
    if ((list != null) && (list.size() > 0))
    {
      Hashtable newsMap = (Hashtable)list.get(0);
      if ((newsMap.get("count") != null) && (!newsMap.get("count").toString().equals(""))) {
        count = Integer.parseInt(newsMap.get("count").toString());
      }
    }
    return count;
  }

  public List getListByPk(String pk)
  {
    return this.queryList.getList("getTi_brandByPk", pk);
  }

  public SysMap brandSort(DbmInterf dbmMgr, SysMap map)
  {
    Ti_brand ti_brand = new Ti_brand();
    String pkid_string = map.getString("sort");
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++)
        {
          if (pkStr[i].equals(""))
            continue;
          String[] sort = pkStr[i].split(",");
          if (sort.length == 1)
          {
            ti_brand.setInfo_id(sort[0]);
            ti_brand.setSort_no(0);
            updateSort(dbmMgr, ti_brand);
          }
          if (sort.length != 2)
            continue;
          ti_brand.setInfo_id(sort[0]);
          ti_brand.setSort_no(Integer.parseInt(sort[1]));
          updateSort(dbmMgr, ti_brand);
        }
      }
    }
    catch (ApplicationException e)
    {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updateSort(DbmInterf dbmMgr, Ti_brand ti_brand) throws ApplicationException {
    dbmMgr.update("updateTi_brandSort", ti_brand);
  }

  public List getListByCat_id(String catIds)
  {
    return this.queryList.getList("getTi_brandByCatId", catIds);
  }

  public String getBrandSelectByCatId(String catIds, String checkId)
  {
    List bList = this.queryList.getList("getTi_brandByCatId", catIds);
    StringBuffer select = new StringBuffer();
    if ((null != bList) && (bList.size() > 0)) {
      for (int i = 0; i < bList.size(); i++) {
        String info_id = ""; String title = "";
        Hashtable bMap = (Hashtable)bList.get(i);
        if (bMap.get("info_id") != null)
        {
          info_id = bMap.get("info_id").toString();
        }
        if (bMap.get("title") != null)
        {
          title = bMap.get("title").toString();
        }
        if (checkId.equals(info_id))
          select.append("<option value='" + info_id + "' selected>" + title + "</option>");
        else {
          select.append("<option value='" + info_id + "'>" + title + "</option>");
        }
      }
    }
    return select.toString();
  }

  public String getBrandSelectAll(String checkId)
  {
    List bList = this.queryList.getList("getTi_brandAll", "");
    StringBuffer select = new StringBuffer();
    if ((null != bList) && (bList.size() > 0)) {
      for (int i = 0; i < bList.size(); i++) {
        String info_id = ""; String title = "";
        Hashtable bMap = (Hashtable)bList.get(i);
        if (bMap.get("info_id") != null)
        {
          info_id = bMap.get("info_id").toString();
        }
        if (bMap.get("title") != null)
        {
          title = bMap.get("title").toString();
        }
        if (checkId.equals(info_id))
          select.append("<option value='" + info_id + "' selected>" + title + "</option>");
        else {
          select.append("<option value='" + info_id + "'>" + title + "</option>");
        }
      }
    }
    return select.toString();
  }
}